import axios from 'axios'
import {getToken} from '@/utils/tooken.js'
import { Message } from 'element-ui'
const service = axios.create({
    baseURL: '/api',
    timeout: 5000
})

// 添加请求拦截器
 service.interceptors.request.use((config)=>{
    // 在请求中写入token
    config.headers['tooken'] = getToken("username")
    return config
 },(error)=>{
    return Promise.reject(error)
 })

// 添加响应拦截器
service.interceptors.response.use((response)=>{
    let { status ,data} = response
    if(status === "200"){
        return response
    }else{
        // 这里要用emementUI的消息提示
        Message({
            message: data || "service请求失败",
            type: 'warning'
        })
    }
    
},(error)=>{
    return Promise.reject(error)
})

export default service